/**
 * 
 */
package com.gmail.blackdog1987.carcare.fragment;

import in.srain.cube.image.CubeImageView;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.gmail.blackdog1987.carcare.App;
import com.gmail.blackdog1987.carcare.R;
import com.gmail.blackdog1987.carcare.activity.LoginActivity;
import com.gmail.blackdog1987.carcare.activity.ProductActivity;
import com.gmail.blackdog1987.carcare.db.dao.UserDao;
import com.gmail.blackdog1987.carcare.entity.TUser;
import com.umeng.analytics.MobclickAgent;

/**
 * @author wdy
 *
 */
public class AccountFragment extends Fragment implements OnClickListener {
	private View mContentView;

	private TUser mUser;
	UserDao mDao;
	CubeImageView mHead;
	TextView mUsername;
	View mLogout;
	AlertDialog logoutDialog;

	public static final int REQUESTCODE_LOGIN = 1;

	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		mContentView = inflater.inflate(R.layout.fragment_account, null);
		mDao = new UserDao();
		mUser = mDao.getUser();
		initViews();
		reg();
		if (mUser == null) {
			mLogout.setVisibility(View.GONE);
		} else {
			mUsername.setText(mUser.username);
			mLogout.setVisibility(View.VISIBLE);
		}
		return mContentView;
	};

	private void initViews() {
		mHead = (CubeImageView) mContentView.findViewById(R.id.account_img);
		mUsername = (TextView) mContentView.findViewById(R.id.account_username);
		mLogout = mContentView.findViewById(R.id.account_logout);
	}

	private void reg() {
		mContentView.findViewById(R.id.account_account).setOnClickListener(this);
		mContentView.findViewById(R.id.account_logout).setOnClickListener(this);
		mContentView.findViewById(R.id.account_msg).setOnClickListener(this);
		mContentView.findViewById(R.id.account_order).setOnClickListener(this);
		mContentView.findViewById(R.id.account_score).setOnClickListener(this);
	}

	public void onResume() {
		super.onResume();
		MobclickAgent.onPageStart("MainScreen"); // 统计页面
		mUser = mDao.getUser();
	}

	public void onPause() {
		super.onPause();
		MobclickAgent.onPageEnd("MainScreen");
	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
		if (resultCode == Activity.RESULT_OK) {
			switch (requestCode) {
			case REQUESTCODE_LOGIN:
				mUser = mDao.getUser();
				mUsername.setText(mUser.username);
				mLogout.setVisibility(View.VISIBLE);
				break;
			}
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.account_account:
			mUser = mDao.getUser();
			if (mUser == null) {
				startActivityForResult(new Intent(getActivity(), LoginActivity.class), REQUESTCODE_LOGIN);
			}
			break;
		case R.id.account_logout:
			if (logoutDialog == null) {
				logoutDialog = new AlertDialog.Builder(getActivity()).setTitle("温馨提示").setMessage("是否注销账户？")
						.setPositiveButton("注销", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								mUser = null;
								mDao.clear();
								mLogout.setVisibility(View.GONE);
								mUsername.setText("请登录");
								Toast.makeText(getActivity(), "已经注销", Toast.LENGTH_LONG).show();
								dialog.dismiss();
							}
						}).setNegativeButton("我按错了", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								dialog.dismiss();
							}
						}).create();
			}
			if (logoutDialog != null && !logoutDialog.isShowing()) {
				logoutDialog.show();
			}
			break;
		case R.id.account_msg:
			Toast.makeText(getActivity(), "敬请期待", Toast.LENGTH_LONG).show();
			break;
		case R.id.account_order:
			if(App.validUser(getActivity())){
				startActivity(new Intent(getActivity(), ProductActivity.class));
			}
			break;
		case R.id.account_score:
			Toast.makeText(getActivity(), "敬请期待", Toast.LENGTH_LONG).show();
			break;
		}
	}

}
